package com.接口;

import java.util.Random;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/2/17 15:57
 * @description:
 * @version:
 */
public class SupplierTest {
    public static void main(String[] args) {
        Supplier<Integer> supplier = new Supplier<Integer>() {
            @Override
            public Integer get() {
                return null;
            }
        };
    }

    public static void testSupplier() {
        //① 使用Supplier接口实现方法,只有一个get方法，无参数，返回一个值

        Supplier<Integer> supplier = new Supplier<Integer>() {
            @Override
            public Integer get() {
                //返回一个随机值
                return new Random().nextInt();
            }
        };

        System.out.println(supplier.get());

        //② 使用lambda表达式，
        supplier = ()-> new Integer(10);
        System.out.println(supplier.get());

        //③ 使用方法引用
        Supplier<Double> supplier2 = Math::random;
        System.out.println(supplier2.get());

        //4. IntSupplier DoubleSupplier LongSupplier 里面只有一个getAsXXX的方法，
        IntSupplier intSupplier = new IntSupplier() {
            @Override
            public int getAsInt() {
                return new Random().nextInt();
            }
        };

        System.out.println(intSupplier.getAsInt());
    }

    public static class Student implements Supplier {
        private String name;
        private Integer age;

        @Override
        public Object get() {
            return age;
        }
    }

}
